﻿using BLL;
using DAL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Model;
using NLog.Web;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;

namespace _11._7WebApI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MessageController : ControllerBase
    {
        public readonly ILogger<LoginController> _logger;
        public MessageController (MessageBLL messageBLL, ILogger<LoginController> logger)
        {
            MessageBLL = messageBLL;
            _logger = logger;
        }
        public MessageBLL MessageBLL { get; }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public int Add(Message message)
        {
            try
            {
                _logger.LogInformation("机构信息添加成功");
                return MessageBLL.Add(message);
            }
            catch (Exception)
            {
                _logger.LogInformation("机构信息添加失败");
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 

        [HttpDelete]
        public int Delete(int id)
        {
            try
            {

                return MessageBLL.Delete(id);
            }
            catch (Exception)
            {
                _logger.LogInformation("机构信息删除失败");
                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        /// 

        [HttpPut]
        public int Update(Message message)
        {
            try
            {

                return MessageBLL.Update(message);
            }
            catch (Exception)
            {
                _logger.LogInformation("机构信息修改失败");
                throw;
            }
        }
        /// <summary>
        /// 反添
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 

        [HttpGet]
        public Message GetMessage(int id)
        {
            try
            {
                return MessageBLL.GetMessage(id);
            }
            catch (Exception)
            {
                _logger.LogInformation("机构信息反添失败");
                throw;
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        /// 
        /// 
        [HttpGet]
        public List<Message> GetAll()
        {
            try
            {
                return MessageBLL.GetAll();
            }
            catch (Exception)
            {
                _logger.LogInformation("机构显示失败");
                throw;
            }
        }
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Pic(IFormFile file)
        {
            string newFile = Directory.GetCurrentDirectory() + @"\wwwroot\Pic\" + file.FileName;
            using (FileStream fs = new FileStream(newFile, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            //输出文件路径
            return Ok("http://localhost:5028/Pic/" + file.FileName);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        public IActionResult GoTpListExcel()
        {
            var list = GetAll();
            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("显示列表");
            IRow row = sheet.CreateRow(0);
            //在表格中写值
            row.CreateCell(0).SetCellValue("机构名称");
            row.CreateCell(1).SetCellValue("机构封面");
            row.CreateCell(2).SetCellValue("特色服务");
            row.CreateCell(3).SetCellValue("营业时间");
            row.CreateCell(4).SetCellValue("机构地址");
            row.CreateCell(5).SetCellValue("联系电话");
            row.CreateCell(6).SetCellValue("发布时间");
            row.CreateCell(7).SetCellValue("状态");
            row.CreateCell(8).SetCellValue("机构详情");

            //循环写值
            int index = 1;
            foreach (var item in list)
            {
                row = sheet.CreateRow(index);
                row.CreateCell(0).SetCellValue(item.Name);
                row.CreateCell(1).SetCellValue(item.Pic);
                row.CreateCell(2).SetCellValue(item.FuWu);
                row.CreateCell(3).SetCellValue(item.YTime);
                row.CreateCell(4).SetCellValue(item.Address);
                row.CreateCell(5).SetCellValue(item.Phone);
                row.CreateCell(6).SetCellValue(item.FTime);
                row.CreateCell(7).SetCellValue(item.start);
                row.CreateCell(8).SetCellValue(item.XiangQing);
                index++;
            }
            byte[] buffer;
            using (MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                buffer = ms.ToArray();
            }
            string excelMima = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(buffer, excelMima, "机构显示列表.xlsx");
        }
        //
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Count"></param>
        /// <param name="Sum"></param>
        /// <param name="Index"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        /// 

        [HttpGet]
        public IActionResult GetShow(string? name,int Index = 1, int Size = 2)
        {
            try
            {
                int Count = 0;
                int Sum = 0;
                var list =  MessageBLL.GetShow(name, out Count, out Sum, Index, Size);
                return Ok(new
                {
                    data = list,
                    Count = Count,
                    Sum = Sum
                });
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
